﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PHTShop
{
    public partial class WebForm7 : basePageUser
    {
        private DataContextDataContext dc = new DataContextDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["productsId"] != null)
                {
                   
                    this.ProductLanguage();
                    this.producRelated();

                }
                switch (Session["languages"].ToString().ToLower())
                {
                    case "france":
                        Page.Title = "Détails";
                        break;
                    case "germany":
                        Page.Title = "Einzelheiten";
                        break;
                    default:
                        Page.Title = "Details";
                        break;
                }
            }
        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
            Setting th = this.dc.Settings.Single(t => t.type.Equals("theme") && Convert.ToInt32(t.value) == 1);
            Page.Theme = th.name.ToString();
        }

        private void ProductLanguage()
        {
            Language lg = this.dc.Languages.Single(t => t.name.Equals(Session["languages"]));

            ProductsLanguage prolg = this.dc.ProductsLanguages.Single(t => t.languageId == lg.id && t.productsId == Convert.ToInt32(Request.QueryString["productsId"]));
            this.LabelProducName.Text = prolg.name;
            this.LiteralDetails.Text = prolg.details;
            this.LiteralMoreDetails.Text = prolg.details;


            Product pro = this.dc.Products.Single(t => t.id == Convert.ToInt32(Request.QueryString["productsId"]));
            this.price.InnerText = pro.price.Value.ToString() + "$";
            this.HiddenField.Value = pro.id.ToString();

            List<ProductsPhoto> productsPhoto = this.dc.ProductsPhotos.Where(t => t.productsId == Convert.ToInt32(Request.QueryString["productsId"])).ToList();
            string photoName = "";
            foreach (var j in productsPhoto)
            {
                ProductsPhoto propt = this.dc.ProductsPhotos.Single(t => t.id == j.id);
                photoName = propt.name;
            }
            this.photoName.Src = photoName;
            this.ahref.HRef = photoName;

            this.RepeaterPictures.DataSource = productsPhoto.Take(3);
            this.RepeaterPictures.DataBind();
        }

        protected void OrderNow_click(object sender, EventArgs e)
        {
            try
            {
                Language lg = this.dc.Languages.Single(t => t.name.Equals(Session["languages"]));

                if (Session["Cart"] == null)
                {
                    List<Items> cart = new List<Items>();

                    Product pro = this.dc.Products.Single(t => t.id == Convert.ToInt32(this.HiddenField.Value));
                    ProductsLanguage prolg = this.dc.ProductsLanguages.Single(t => t.languageId == lg.id && t.productsId == Convert.ToInt32(this.HiddenField.Value));
                    List<ProductsPhoto> productsPhoto = this.dc.ProductsPhotos.Where(t => t.productsId == Convert.ToInt32(this.HiddenField.Value)).ToList();
                    string photoName = "";
                    foreach (var j in productsPhoto)
                    {
                        ProductsPhoto propt = this.dc.ProductsPhotos.Single(t => t.id == j.id);
                        photoName = propt.name;
                    }
                    cart.Add(new Items(pro, 1, prolg, photoName));
                    Session["Cart"] = cart;
                }
                else
                {
                    List<Items> cart = (List<Items>)Session["Cart"];
                    int id = Convert.ToInt32(this.HiddenField.Value);
                    int index = this.isExist(id);
                    if (index != -1)
                    {
                        cart[index].qua++;
                    }
                    else
                    {
                        Product pro = this.dc.Products.Single(t => t.id == Convert.ToInt32(this.HiddenField.Value));
                        ProductsLanguage prolg = this.dc.ProductsLanguages.Single(t => t.languageId == lg.id && t.productsId == Convert.ToInt32(this.HiddenField.Value));
                        List<ProductsPhoto> productsPhoto = this.dc.ProductsPhotos.Where(t => t.productsId == Convert.ToInt32(this.HiddenField.Value)).ToList();
                        string photoName = "";
                        foreach (var j in productsPhoto)
                        {
                            ProductsPhoto propt = this.dc.ProductsPhotos.Single(t => t.id == j.id);
                            photoName = propt.name;
                        }

                        cart.Add(new Items(pro, 1, prolg, photoName));
                    }
                    Session["Cart"] = cart;
                }
                Response.Redirect("Cart.aspx");
            }
            catch
            {

            }
        }

        private int isExist(int id)
        {
            List<Items> cart = (List<Items>)Session["Cart"];
            for (int i = 0; i < cart.Count; i++)
            {
                if (cart[i].pro.id == id)
                {
                    return i;
                }
            }
            return -1;
        }

        private void producRelated()
        {
            List<ProductsRelative> prorl = this.dc.ProductsRelatives.Where(t => t.productsId_1 == Convert.ToInt32(Request.QueryString["productsId"])).ToList();

            Language lg = this.dc.Languages.Single(t => t.name.Equals(Session["languages"]));


            List<ClassRelatives> list = new List<ClassRelatives>();

            foreach (ProductsRelative i in prorl)
            {
                try
                {
                    Product pro = this.dc.Products.Single(t => t.id == i.productsId_2 && t.quantity > 0 && t.status == true);
                    ProductsLanguage prolg = this.dc.ProductsLanguages.Single(t => t.productsId == i.productsId_2 && t.languageId == lg.id);

                    string photoName = "";
                    List<ProductsPhoto> productsPhoto = this.dc.ProductsPhotos.Where(t => t.productsId == i.productsId_2).ToList();
                    foreach (var j in productsPhoto)
                    {
                        ProductsPhoto propt = this.dc.ProductsPhotos.Single(t => t.id == j.id);
                        photoName = propt.name;
                    }
                    list.Add(new ClassRelatives(pro, prolg, photoName));
                }
                catch
                {

                }
            }

            this.RepeaterRelated.DataSource = list.Take(6);
            this.RepeaterRelated.DataBind();
        }
    }
}